﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection.Emit;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;
using Label = System.Web.UI.WebControls.Label;

namespace HRM.UI.HRManagerUI.PunishmentUI
{
    public partial class PunishedEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulatePunishedType();
                GetAllProcedure();
                ViewAllPunishedEmployee();
            }
        }

        private void ViewAllPunishedEmployee()
        {
            List<ViewAllPunisedmentEmployee> viewAllPunisedmentEmployees = new List<ViewAllPunisedmentEmployee>();
            PunishmentManager manager = new PunishmentManager();
            viewAllPunisedmentEmployees = manager.GetAllPunishedEmployee();
            viewPunishedEmployeeGdv.DataSource = viewAllPunisedmentEmployees;
            viewPunishedEmployeeGdv.DataBind();
            countLbl.Text ="Total Result Found:"+ viewAllPunisedmentEmployees.Count.ToString();
        }

        private void PopulatePunishedType()
        {
            try
            {
                List<PunishmentType> punishmentTypes = new List<PunishmentType>();
                PunishmentTypeManager manager = new PunishmentTypeManager();
                punishmentTypes = manager.GetAllPunishmentType();
                selectPunishmentType.DataValueField = "Id";
                selectPunishmentType.DataTextField = "Type";
                selectPunishmentType.DataSource = punishmentTypes;
                selectPunishmentType.DataBind();
                selectPunishmentType.Items.Insert(0, "---Choose Any---");

                selectSearchByType.DataValueField = "Id";
                selectSearchByType.DataTextField = "Type";
                selectSearchByType.DataSource = punishmentTypes;
                selectSearchByType.DataBind();
                selectSearchByType.Items.Insert(0, "---Choose Any---");
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void GetAllProcedure()
        {
            try
            {
                PunishedProcedureManager manager = new PunishedProcedureManager();
                List<PunishedProcedure> procedures = new List<PunishedProcedure>();
                procedures = manager.GetAllProcedure();
                selectPunishedProcedure.DataValueField = "Id";
                selectPunishedProcedure.DataTextField = "Procedure";
                selectPunishedProcedure.DataSource = procedures;
                selectPunishedProcedure.DataBind();
                selectPunishedProcedure.Items.Insert(0, "---Choose Any---");
            }
            catch (Exception)
            {
                throw;
            }
        }


        protected void txtEmployeeCode_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ViewEmployee employee = new ViewEmployee();
                EmployeeManager employeeManager = new EmployeeManager();
                Code code = new Code();
                code.CodeName = txtEmployeeCode.Text;
                employee = employeeManager.SearchEmployeeByCode(code);
                empNameLbl.Text = "[Name of Emp: " + employee.Employee.Name +"<br/> Designation: " + employee.Designation.Title + "]";
                empNameLbl.ForeColor = Color.Green;
            }
            catch (Exception)
            {

                empNameLbl.Text = "";
            }
        }

        protected void CreatePunishment(object o, EventArgs e)
        {
            try
            {
                Punishment punishment = new Punishment();
                Employee punishedEmployee = new Employee();
                Employee punishedBy = new Employee();
                Code punishedEmployeeCode = new Code();
                Code punishedByCode = new Code();
                PunishedProcedure procedure = new PunishedProcedure();
                PunishmentType type = new PunishmentType();

                punishedEmployeeCode.CodeName = txtEmployeeCode.Text;
                punishedEmployee.EmpCode = punishedEmployeeCode;
                punishment.Employee = punishedEmployee;

                type.Id = Convert.ToInt32(selectPunishmentType.Value);
                punishment.PunishdType = type;

                procedure.Id = Convert.ToInt32(selectPunishedProcedure.Value);
                punishment.Procedure = procedure;

                punishment.Date = Convert.ToDateTime(txtDate.Value);
                punishment.Remarks = txtRemarks.Value;

                // punishedByCode.CodeName = txtPunishedBy.Text;
                // punishedBy.EmpCode = punishedByCode;
                punishment.PunishedBy = punishedByNameLbl.Text;
                punishment.Status = "ACTIVE";
                PunishmentManager manager = new PunishmentManager();
                if (manager.ApplyPunishment(punishment))
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Succefully Applied Punishment to Employee";
                    ViewAllPunishedEmployee();
                }
                else
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Employee Already has Active Punishment";
                }

            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }

        protected void txtPunishedBy_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ViewEmployee employee = new ViewEmployee();
                EmployeeManager employeeManager = new EmployeeManager();
                Code code = new Code();
                code.CodeName = txtPunishedBy.Text;
                employee = employeeManager.SearchEmployeeByCode(code);
                punishedByNameLbl.Text = employee.Employee.Name + " - " + employee.Designation.Title + "";
                punishedByNameLbl.ForeColor = Color.Green;
            }
            catch (Exception)
            {

                punishedByNameLbl.Text = "";
            }
        }

        protected void viewPunishedEmployeeGdv_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            viewPunishedEmployeeGdv.PageIndex = e.NewPageIndex;
            ViewAllPunishedEmployee();
        }

        protected void FindPunishedEmployee(object o,EventArgs e)
        {
            try
            {
                List<ViewAllPunisedmentEmployee> viewAllPunisedmentEmployees = new List<ViewAllPunisedmentEmployee>();
                PunishmentManager manager = new PunishmentManager();
                Code code = new Code();
                PunishmentType type = new PunishmentType();
                code.CodeName = txtSearchByCode.Value;
                type.Type = selectSearchByType.SelectedItem.Text;

                if (code.CodeName != "")
                {
                    viewAllPunisedmentEmployees = manager.GetAllPunishedEmployee(code);
                    txtSearchByCode.Value = "";

                }
                else if (type.Type != "--Choose Any--")
                {
                    viewAllPunisedmentEmployees = manager.GetAllPunishedEmployee(type);
                }

                viewPunishedEmployeeGdv.DataSource = viewAllPunisedmentEmployees;
                viewPunishedEmployeeGdv.DataBind();
                countLbl.Text = "Total Result Found:" + viewAllPunisedmentEmployees.Count.ToString();
               
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void GetAll(object o,EventArgs e)
        {
            ViewAllPunishedEmployee();
        }

        protected void viewPunishedEmployeeGdv_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
        {
            try
            {
                PunishmentManager manager=new PunishmentManager();
                PunishedProcedureManager punishedProcedure = new PunishedProcedureManager();
                int id = Convert.ToInt32(((Label)viewPunishedEmployeeGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
                string msg = manager.DeletePunish(id);
                if (msg.Contains("Successfully"))
                {
                    lblDelete.Text = "Punished Procedure Successfully deleted";
                    lblDelete.ForeColor = Color.Green;
                    ViewAllPunishedEmployee();

                }
                else
                {
                    lblDelete.Text = "sorry !Punished Procedure not deleted";
                    lblDelete.ForeColor = Color.Red;
                }

            }
            catch (Exception)
            {
                lblDelete.Text = "sorry !Punished Procedure not deleted";
                lblDelete.ForeColor = Color.Red;
            }
        }

        //protected void viewPunishedEmployeeGdv_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
        //{
        //    try
        //    {
        //        PunishmentManager punishmentManager=new PunishmentManager();
        //        Punishment aPunishment=new Punishment();
        //        int id = Convert.ToInt32(((Label)viewPunishedEmployeeGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
        //        msgLbl.Text=punishmentManager.

        //    }
        //    catch (Exception)
        //    {
                
        //        throw;
        //    }
        //}
    }
}